/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-04-13
 * Time:21:34
 */

/**
 * BM2链表内指定区间反转
 */

public class reverseBetween {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        // write code here
        if (head == null) {
            return null;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = pre.next;
        for (int i = 1; i < m; i++) {
            pre = pre.next;
            cur = cur.next;
        }
        for (int i = m; i < n; i++) {
            ListNode tmp = cur.next;
            cur.next = tmp.next;
            tmp.next = pre.next;
            pre.next = tmp;
        }
        return dummy.next;
    }
}
